package com.trippin.Utils.unittests;

import static org.junit.Assert.*;

import java.text.ParseException;
//import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;


import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.trippin.Utils.TimeUtil;
import com.trippin.Utils.TrippinDateJsonDeserializer;
import com.trippin.Utils.TrippinDateJsonSerializer;
import com.trippin.server.TrippinDateTime;

public class TimeUtilTest {

	@Test
	public void convertToCalendarTest() {
		TrippinDateTime cal = new TrippinDateTime(2011,12,3,2,12,-2);
		
		TrippinDateTime calCreated = TimeUtil.convertToTrippinDateTime("3/12/2011 00002:12 -0002");
		assertTrue(cal.equals(calCreated));
		
		calCreated = TimeUtil.convertToTrippinDateTime("   3/12/2011 02:12   -02 ");
		assertTrue(cal.equals(calCreated));
		
		calCreated = TimeUtil.convertToTrippinDateTime("3/12/2011     02:12 -02");
		assertTrue(cal.equals(calCreated));
		
		calCreated = TimeUtil.convertToTrippinDateTime("3/ 12/ 201102:12 -2");
		assertFalse(cal.equals(calCreated));
	}
	
	@Test
	public void convertToTrippinDateTimeTest() {
		String calendarJson = "{ year : 1999," +
				"month : 1, " +
				"day : 1, " +
				"hour : 2, " +
				"minutes : 50, " +
				"GMT : -01" +
				"}";
		
		Gson gson = new Gson();
		TrippinDateTime tdt = gson.fromJson(calendarJson, TrippinDateTime.class);
		assertEquals("01/01/1999 02:50 -01", tdt.toString());
	}
	
	@Test
	public void JavaUtilDateTest() throws ParseException {
		//SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");

		//Date date = sdf.parse("30/12/2011     16:40");
		TimeZone tz = new SimpleTimeZone(7200000 , "bla");
		
		//Calendar cal = Calendar.getInstance(new SimpleTimeZone(0 , "blu"));
		
		Calendar calFromDate = Calendar.getInstance(tz);
		calFromDate.set(2011, 12, 30, 16, 40);
		
		//System.out.println("cal: " + cal.getTime().toString());
		//System.out.println("date: " + date.toString());
		//System.out.println(cal.before(calFromDate));
		/*
		Calendar cal = Calendar.getInstance(); 
		cal.setTime(date) ;
		
		Gson gson = new Gson();
		System.out.println(gson.toJson(cal));
		
		String dateDtring = "{year:2012,dayOfMonth:2,hourOfDay:2,minute:45,month:1}";
		cal = gson.fromJson(dateDtring, Calendar.class);
		System.out.println(gson.toJson(cal));
		dateDtring = null;
		cal = gson.fromJson(dateDtring, Calendar.class);
		if(cal == null)
			System.out.println("cal is null");
			*/
	}
	
	@Test
	public void testDateParser() {
		//SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		String json = "{\"date\": \"30/12/2011             16:40\"}";
		GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(Date.class, new TrippinDateJsonDeserializer());
		Gson gson = gsonBuilder.create();
		DateTesting date = gson.fromJson(json, DateTesting.class);
		System.out.println(date.getDate().toString());
		
		DateTesting toString = new DateTesting();
		toString.setDate(new Date());
		
		gsonBuilder = new GsonBuilder().registerTypeAdapter(Date.class, new TrippinDateJsonSerializer());
		Gson gson2 =  gsonBuilder.create();		
		System.out.println(gson2.toJson(toString));
	}
	
	private static class DateTesting {
		private Date date;

		public Date getDate() {
			return date;
		}

		public void setDate(Date date) {
			this.date = date;
		}
	}
	
}
